export default {
  namespaced: true,
  state: {
    // 是否登录
    loggedIn: false,
    // 用户类型
    userType: 0,
    // 路由
    roles: [],
    // 权限
    permission: 0
  },
  mutations: {
    updateLoggedIn(state, val = true) {
      state.loggedIn = val
    },
    updateUserType(state, val) {
      state.userType = val
    },
    // 修改路由
    updateRoles(state, val) {
      state.roles = val
    },
    // 修改权限
    updatepermission(state, val) {
      state.permission = val
    }
  },
  // 异步操作用于权限组设置
  actions: {
    asyncRoles(store, val) {
      return new Promise((resolve) => {
        let group = []
        for (let bit = 0; bit < 8; bit++) {
          // 0100 0000
          // 0010 0000
          // 位运算
          if (((1 << bit) & store.state.permission) === 1 << bit) {
           group.push(1 << bit)
          }
        }
        let newRoles=val.filter(item=>{
            return group.includes(item.children[0].meta.role)
        })
        store.commit('updateRoles',newRoles)
        resolve(newRoles)
      })
    }
  }
}
